# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=typst
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.7.0
pkgrel=1
pkgdesc='A markup-based typesetting system for the sciences (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://typst.app/'
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/typst/typst/archive/refs/tags/v${pkgver}.tar.gz")
sha256sums=('550fb66bb3405951dfd0a1736e9e17756e906e664f6f683eeb87d40643218846')

prepare() {
  cd ${_realname}-${pkgver}

  cargo vendor \
    --locked \
    --versioned-dirs

  mkdir -p .cargo
  cat >> .cargo/config.toml <<END

[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
END


  sed -i -e 's/"cfg(all(target_arch = \\"\([^\\]\+\)\\", target_env = \\"gnu\\", target_abi = \\"llvm\\", not(windows_raw_dylib)))"/"\1-pc-windows-gnullvm"/g' \
    "vendor/windows-targets-0.48.0/Cargo.toml"
  sed -i -e 's/\("Cargo.toml":\)"[^"]\+"/\1"'"$(sha256sum "vendor/windows-targets-0.48.0/Cargo.toml" | cut -d' ' -f1)"'"/' \
    "vendor/windows-targets-0.48.0/.cargo-checksum.json"
}

build() {
  cd ${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cargo build -p typst-cli --frozen --release --all-features
}

package() {
  cd ${_realname}-${pkgver}
  install -Dm0755 -t "${pkgdir}${MINGW_PREFIX}/bin/" "target/release/typst.exe"
}
